package com.whl_Holden.minio.controller;

import java.io.IOException;
import java.util.List;

import com.whl_Holden.minio.entity.FileLink;
import com.whl_Holden.minio.entity.UploadFile;
import com.whl_Holden.minio.service.MinioService;
import com.whl_Holden.minio.utils.MinioUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author 吴浩炼
 * @since 2024/05/29
 */
@RestController
@RequestMapping("/minio")
public class MinioController {

    @Autowired
    private MinioService minioService;

    @Autowired
    private MinioUtil minioUtil;

    Logger logger = LoggerFactory.getLogger(MinioUtil.class);


    @GetMapping("/checkExits")
    public ResponseEntity<Boolean> checkExits(@RequestParam String md5, @RequestParam String fileSuffix) {
        return ResponseEntity.ok(minioUtil.checkExist(md5, fileSuffix));
    }

    @PostMapping("/upload")
    public ResponseEntity<Boolean> upload(@RequestParam String md5, @RequestParam Integer chunkIndex, @RequestParam MultipartFile chunk)
            throws IOException {
        return ResponseEntity.ok(minioUtil.upload(md5, chunkIndex, chunk.getInputStream(), chunk.getSize()));
    }

    @PostMapping("/merge")
    public ResponseEntity<String> merge(@RequestParam String md5, @RequestParam Integer chunkTotal, @RequestParam String fileSuffix) {
        return ResponseEntity.ok(minioUtil.merge(md5, chunkTotal, fileSuffix) ? "成功！" : "失败！");
    }

    @GetMapping("/list")
    public ResponseEntity<List<FileLink>> list() {
        return ResponseEntity.ok(minioUtil.getFileLinkList());
    }

    @GetMapping("/delete")
    public ResponseEntity<Boolean> delete(@RequestParam String url) {
        System.out.println("url34:" + url);
        return ResponseEntity.ok(minioUtil.deleteFileLink(url));
    }

    @PostMapping("/checkMd5List")
    public ResponseEntity<List<String>> checkFilesExits(@RequestBody List<UploadFile> genericItems){
        return ResponseEntity.ok(minioUtil.checkFilesExits(genericItems));
    }
}
